from tkinter import *
from tkinter.ttk import *
class WinGUI(Tk):
    def __init__(self):
        super().__init__()
        self.__win()
        self.tk_table_result = self.__tk_table_result(self)
        self.tk_frame_top = self.__tk_frame_top(self)
        self.tk_label_local_port = self.__tk_label_local_port( self.tk_frame_top) 
        self.tk_input_local_port = self.__tk_input_local_port( self.tk_frame_top) 
        self.tk_label_local_host = self.__tk_label_local_host( self.tk_frame_top) 
        self.tk_input_local_host = self.__tk_input_local_host( self.tk_frame_top) 
        self.tk_label_remote_host = self.__tk_label_remote_host( self.tk_frame_top) 
        self.tk_input_remote_port = self.__tk_input_remote_port( self.tk_frame_top) 
        self.tk_input_remote_host = self.__tk_input_remote_host( self.tk_frame_top) 
        self.tk_label_remote_port = self.__tk_label_remote_port( self.tk_frame_top) 
        self.tk_button_submit = self.__tk_button_submit(self)
        self.tk_button_reflesh = self.__tk_button_reflesh(self)
    def __win(self):
        self.title("WSL端口转发工具")
        # 设置窗口大小、居中
        width = 831
        height = 655
        screenwidth = self.winfo_screenwidth()
        screenheight = self.winfo_screenheight()
        geometry = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        self.geometry(geometry)
        
        self.resizable(width=False, height=False)
        
    def scrollbar_autohide(self,vbar, hbar, widget):
        """自动隐藏滚动条"""
        def show():
            if vbar: vbar.lift(widget)
            if hbar: hbar.lift(widget)
        def hide():
            if vbar: vbar.lower(widget)
            if hbar: hbar.lower(widget)
        hide()
        widget.bind("<Enter>", lambda e: show())
        if vbar: vbar.bind("<Enter>", lambda e: show())
        if vbar: vbar.bind("<Leave>", lambda e: hide())
        if hbar: hbar.bind("<Enter>", lambda e: show())
        if hbar: hbar.bind("<Leave>", lambda e: hide())
        widget.bind("<Leave>", lambda e: hide())
    
    def v_scrollbar(self,vbar, widget, x, y, w, h, pw, ph):
        widget.configure(yscrollcommand=vbar.set)
        vbar.config(command=widget.yview)
        vbar.place(relx=(w + x) / pw, rely=y / ph, relheight=h / ph, anchor='ne')
    def h_scrollbar(self,hbar, widget, x, y, w, h, pw, ph):
        widget.configure(xscrollcommand=hbar.set)
        hbar.config(command=widget.xview)
        hbar.place(relx=x / pw, rely=(y + h) / ph, relwidth=w / pw, anchor='sw')
    def create_bar(self,master, widget,is_vbar,is_hbar, x, y, w, h, pw, ph):
        vbar, hbar = None, None
        if is_vbar:
            vbar = Scrollbar(master)
            self.v_scrollbar(vbar, widget, x, y, w, h, pw, ph)
        if is_hbar:
            hbar = Scrollbar(master, orient="horizontal")
            self.h_scrollbar(hbar, widget, x, y, w, h, pw, ph)
        self.scrollbar_autohide(vbar, hbar, widget)
    def __tk_table_result(self,parent):
        # 表头字段 表头宽度
        columns = {"ID":73,"本地地址":184,"本地端口":147,"远程地址":184,"远程端口":147}
        tk_table = Treeview(parent, show="headings", columns=list(columns),)
        for text, width in columns.items():  # 批量设置列属性
            tk_table.heading(text, text=text, anchor='center')
            tk_table.column(text, anchor='center', width=width, stretch=False)  # stretch 不自动拉伸
        
        tk_table.place(x=50, y=240, width=739, height=412)
        return tk_table
    def __tk_frame_top(self,parent):
        frame = Frame(parent,)
        frame.place(x=50, y=10, width=738, height=129)
        return frame
    def __tk_label_local_port(self,parent):
        label = Label(parent,text="本地端口",anchor="center", )
        label.place(x=0, y=70, width=62, height=36)
        return label
    def __tk_input_local_port(self,parent):
        ipt = Entry(parent, )
        ipt.place(x=70, y=70, width=239, height=37)
        return ipt
    def __tk_label_local_host(self,parent):
        label = Label(parent,text="本地主机",anchor="center", )
        label.place(x=0, y=0, width=62, height=36)
        return label
    def __tk_input_local_host(self,parent):
        ipt = Entry(parent, )
        ipt.place(x=70, y=0, width=238, height=37)
        return ipt
    def __tk_label_remote_host(self,parent):
        label = Label(parent,text="远程主机",anchor="center", )
        label.place(x=430, y=0, width=62, height=36)
        return label
    def __tk_input_remote_port(self,parent):
        ipt = Entry(parent, )
        ipt.place(x=510, y=70, width=228, height=37)
        return ipt
    def __tk_input_remote_host(self,parent):
        ipt = Entry(parent, )
        ipt.place(x=510, y=0, width=228, height=37)
        return ipt
    def __tk_label_remote_port(self,parent):
        label = Label(parent,text="远程端口",anchor="center", )
        label.place(x=430, y=70, width=62, height=36)
        return label
    def __tk_button_submit(self,parent):
        btn = Button(parent, text="增加", takefocus=False,)
        btn.place(x=350, y=160, width=140, height=42)
        return btn
    def __tk_button_reflesh(self,parent):
        btn = Button(parent, text="📶", takefocus=False,)
        btn.place(x=730, y=210, width=50, height=30)
        return btn